package com.ruoyi.receipt.utils;

import com.ruoyi.common.constant.RedisConstants;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class StatisAnalysisUtils {

    @Autowired
    private RedisCache redisCache;

    public String getDateStr(){
        //日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//
        //获取最新的日期,
        Date date = new Date();
        //获取字符串格式的当天的日期
        String nowDate = sdf.format(date);  // 输出结果: 如:2021-03-31
        return nowDate;
    }

    /**
     * 批量推入题簇和例题关系
     *
     * @param clusteId 用户id
     * @param subject 题目列表
     * @return
     */
    public void putClusteSubjecRelation(Long clusteId,List<Long> subject) {
        redisCache.bathZsetByListScoreRandom(RedisConstants.CLUSTE_SUBJEC_RELATION+clusteId,subject);
    }

    /**
     * 单条推入题簇和例题关系
     *
     * @param clusteId 用户id
     * @param subject 题目列表
     * @return
     */
    public void putClusteSubjecRelation(Long clusteId,Long subject,Double score) {
        redisCache.zSetAdd(RedisConstants.CLUSTE_SUBJEC_RELATION+clusteId,subject,score);
    }

    /**
     * 推入今日所有题目
     *
     * @param userId 用户id
     * @param subject 题目列表
     * @return
     */
    public void putTodayAllSubject(Long userId,List<Long> subject) {
        redisCache.bathZsetByList(RedisConstants.TODAY_ALL_SUBJECT+getDateStr()+":"+userId,subject);

        //统计今日做过的所有题目个数,解决zset自动去重问题
        redisCache.zSetIncrScore(RedisConstants.TODAY_ALL_SUBJECT_NUM+getDateStr(),userId,1D);
    }

    /**
     * 推入今日所有错题
     *
     * @param userId 用户id
     * @param subject 题目列表
     * @return
     */
    public void putTodayWrongSubject(Long userId,List<Long> subject) {
        redisCache.bathZsetByList(RedisConstants.TODAY_WRONG_SUBJECT+getDateStr()+":"+userId,subject);

        //统计今日做过的所有错题个数,解决zset自动去重问题
        redisCache.zSetIncrScore(RedisConstants.TODAY_WRONG_SUBJECT_NUM+getDateStr(),userId,1D);
    }

    /**
     * 推入今日所有正确题
     *
     * @param userId 用户id
     * @param subject 题目列表
     * @return
     */
    public void putTodayRightSubject(Long userId,List<Long> subject) {
        redisCache.bathZsetByList(RedisConstants.TODAY_RIGHT_SUBJECT+getDateStr()+":"+userId,subject);

        //统计今日所有正确题个数,解决zset自动去重问题
        redisCache.zSetIncrScore(RedisConstants.TODAY_RIGHT_SUBJECT_NUM+getDateStr(),userId,1D);
    }

    /**
     * 推入难度大于35的题目集合
     *
     * @param subjectId 题目id
     */
    public void putDiffictRatioGreater35(Long subjectId) {
        redisCache.zSetAdd(RedisConstants.DIFFICT_RATIO_GREATER_35,subjectId,new Integer(0).doubleValue());
    }

    /**
     * 查看subjectId是否在难度大于35的题目集合中
     * @param subjectId 题目id
     */
    public Boolean getIsExitDiffictRatioGreater35(Long subjectId){
        return redisCache.zSetIsMember(RedisConstants.DIFFICT_RATIO_GREATER_35,subjectId);
    }

    /**
     * 删除修改后难度小于35的题目
     *
     * @param subjectId 题目id
     */
    public void deleteDiffictRatioGreater35(Long subjectId) {
        redisCache.zSetRemove(RedisConstants.DIFFICT_RATIO_GREATER_35,subjectId);
    }

    /**
     * 推入今天学习时长
     *
     * @param userId 用户id
     * @param time 学习时间
     */
    public void putTodayLearnTime(long userId,Integer time) {
        redisCache.zSetIncrScore(RedisConstants.TODAY_LEARN_TIME+getDateStr(),userId,time.doubleValue());
    }

    /**
     * 推入今天切出次数
     *
     * @param userId 用户id
     */
    public void putTodayCutOutNumber(long userId) {
        redisCache.zSetIncrScore(RedisConstants.TODAY_CUT_OUT_NUMBER+getDateStr(),userId,new Integer(1).doubleValue());
    }

    /**
     * 推入今日切出时长
     *
     * @param userId 用户id
     * @param time 切出时长
     */
    public void putTodayCutOutTime(long userId,Integer time) {
        redisCache.zSetIncrScore(RedisConstants.TODAY_CUT_OUT_TIME+getDateStr(),userId,time.doubleValue());
    }

    /**
     * 根据日期查询,用户做过的难度大于35的所有题目个数
     *
     * @param userId 用户id
     * @param DateStr 日期 2021-03-31
     */
    public Long getDifGre35AllNum(Long userId,String DateStr){
        Long size=redisCache.zSetIntersection(RedisConstants.DIFFICT_RATIO_GREATER_35,RedisConstants.TODAY_ALL_SUBJECT+DateStr+":"+userId,"temp:all:"+RedisConstants.DIFFICT_RATIO_GREATER_35+":"+userId);
        return size;
    }

    /**
     * 根据日期查询,用户做过的难度大于35的正确的题目个数
     *
     * @param userId 用户id
     * @param DateStr 日期 2021-03-31
     */
    public Long getDifGre35RightNum(Long userId,String DateStr){
        Long size=redisCache.zSetIntersection(RedisConstants.DIFFICT_RATIO_GREATER_35,RedisConstants.TODAY_RIGHT_SUBJECT+DateStr+":"+userId,"temp:right:"+RedisConstants.DIFFICT_RATIO_GREATER_35+":"+userId);
        return size;
    }

    /**
     * 绑定题簇和题目,题簇作为id,例题作为里面的内容
     *
     * @param clusteId 题簇id
     * @param subject 例题id集合
     */
    public void setClusteSbujectRelation(Long clusteId,List<Long> subject){
        redisCache.bathZsetByList(RedisConstants.CLUSTE_SUBJEC_RELATION+clusteId,subject);
    }


}
